package com.hhl.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.mapper.CommentMapper;
import com.hhl.flower.mapper.OrderDetailMapper;
import com.hhl.flower.mapper.OrdersMapper;
import com.hhl.flower.mapper.UserMapper;
import com.hhl.flower.pojo.entity.Comment;
import com.hhl.flower.pojo.entity.OrderDetail;
import com.hhl.flower.pojo.entity.Orders;
import com.hhl.flower.pojo.entity.User;
import com.hhl.flower.pojo.vo.CommentVO;
import com.hhl.flower.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author Liubuzhu
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2024-06-12 14:38:40
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private OrderDetailMapper orderDetailMapper;

	public List<CommentVO> listByFlower(Integer flowerId) {
		QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("f_id", flowerId);
        List<Comment> comments = commentMapper.selectList(queryWrapper);
		List<CommentVO> commentVOList = new ArrayList<>();
		for (Comment comment : comments) {
			QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.eq("id", comment.getUId());
			User user = userMapper.selectOne(queryWrapper1);
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment,commentVO);
            commentVO.setAvatar(user.getAvatar());
			commentVO.setNickname(user.getNickname());
            commentVOList.add(commentVO);
		}
        return commentVOList;
	}

	public List<CommentVO> listByUser(Integer userId) {
		QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("u_id", userId);
		List<Comment> comments = commentMapper.selectList(queryWrapper);
		List<CommentVO> commentVOList = new ArrayList<>();
		for (Comment comment : comments) {
			QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.eq("id", userId);
			User user = userMapper.selectOne(queryWrapper1);
			CommentVO commentVO = new CommentVO();
			BeanUtils.copyProperties(comment,commentVO);
			commentVO.setAvatar(user.getAvatar());
			commentVO.setNickname(user.getNickname());
			commentVOList.add(commentVO);
		}
		return commentVOList;
	}

	public void addComment(String content, Integer stars, String number) {
		QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("number", number);
		Integer orderId = ordersMapper.selectOne(queryWrapper).getId();
		QueryWrapper<OrderDetail> queryWrapper1 = new QueryWrapper<>();
		queryWrapper1.eq("o_id", orderId);
		List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper1);
		for (OrderDetail orderDetail : orderDetails) {
			Integer userId = BaseContext.getCurrentId();
			Comment comment = new Comment();
			comment.setContent(content);
			comment.setScore(stars);
			comment.setFId(orderDetail.getFId());
			comment.setIsTop(0);
			comment.setCreateTime(LocalDateTime.now());

			comment.setUId(userId);
			commentMapper.insert(comment);
		}

	}

}




